package com.dfiov.cloud.msmtools.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author gaopin
 */

@Data
@ApiModel(value = "Result对象",description = "Result对象")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码")
    private int code;

    @ApiModelProperty(value = "是否成功")
    private boolean success;

    @ApiModelProperty(value = "承载数据")
    private T data;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    private Result(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = HttpStatus.OK.value() == code;
    }


    public static <T> Result<T> data(T data) {
        return data(data, "操作成功");
    }

    public static <T> Result<T> data(T data, String msg) {
        return data(HttpStatus.OK.value(), data, msg);
    }

    public static <T> Result<T> data(int code, T data, String msg) {
        return new Result(code, data, data == null ? "暂无承载数据" : msg);
    }

    public static <T> Result<T> fail(String msg) {
        return fail(HttpStatus.BAD_REQUEST.value(), msg);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return new Result(code, (Object)null, msg);
    }

    public static <T> Result<T> fail() {
        return fail(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    public static <T> Result<T> success() {
        return data(null, "操作成功");
    }
}
